var app = getApp();
var time_box = '';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,
    data: "",
    my_info: "",
    user_open: 0,  //0:自己进入  1:好友进入 3失败 4自己第二次进入
    language: 20,
    shorten: true,  //规则模态框
    opens: true,   //开启成功的集福模态框
    sentiment: true,  //填写祝福语模态框
    please: true, //点赞好友数量模态框
    one: '首获一份优惠券',
    coupon_info: "",
    blessing_comment: "",
    id: "",
    sign: "",
    title: "",
    image:{},
  },
  //开启集福
  sets: function () {
    console.log('-----------------------------');
    var that = this;
    var user_open = that.data.user_open;
    wx.request({
      url: app.url + 'api_blessing/add_blessing',
      data: {
        id: that.data.id,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (res) {
        console.log(res)
        if (res.data.done) {
          that.setData({
            coupon_info: res.data.result.coupon_info,
            opens: false,
            please: false,
            sign: res.data.result.sign,
          })
        }
        var option = {};
        option.id = that.data.id;
        that.onLoad(option);

      }
    })
  },

  exit: function () {
    var that = this;
    var user_open = that.data.user_open;
    that.setData({
      opens: true,
      user_open: 0,
      please: false
    })
  },

  // 查看规则
  read: function () {
    var that = this;
    that.setData({
      shorten: false
    })
  },

  // 填写祝福语
  sends: function () {
    var that = this;
    for (var i in that.data.blessing_comment) {
      if (that.data.blessing_comment[i].user_id == that.data.join_user_id) {
        wx.showModal({
          title: '提示',
          content: '已为Ta点过赞了',
        })
        return;
      }
    }

    wx.request({
      url: app.url + 'api_blessing/check_blessing_number',
      data: {
        id: that.data.id,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (res) {
        if(res.data.result == '成功'){
          that.setData({
            sentiment: false
          })
        }else{
          wx.showModal({
            title: '提示',
            content: '您的祝福次数已达上限'
          })
        }
      },
    })

  },

  out: function () {
    var that = this;
    that.setData({
      sentiment: true,
      shorten: true
    })
  },
  //发送祝福语
  send_bless: function (e) {
    console.log(e);
    var that = this;
    var value = e.detail.value.language;
    var sentiment = that.data.sentiment;
    console.log(value);
    if (value == '') {
      sentiment = false
      wx.showToast({
        title: '失败',
        icon: 'loading',
        duration: 2000
      })
    } else {
      console.log(that.data.sign);
      wx.request({
        url: app.url + 'api_blessing/add_blessing_comment',
        data: {
          id: that.data.id,
          sign: that.data.sign,
          text: value,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          console.log(res)
          sentiment = true;
          if (res.data.done) {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                var option = {};
                option.id = that.data.id;
                option.sign = that.data.sign;
                that.onLoad(option);
              },
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message
            })
          }
          that.setData({ sentiment: true, })

        }
      })

    }
    that.setData({
      sentiment: sentiment
    })
  },

  // 猜你喜欢列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    app.app_load(load_fn);
    
    function load_fn() {
      console.log(options);
      console.log(wx.getStorageSync('token_key'));
      wx.request({
        url: app.url + 'api_blessing/blessing',
        data: {
          id: options.id,
          sign: (options.sign == undefined ? '' : options.sign),
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          if (res.data.done) {
            var user_open = res.data.result.user_open;
            if (user_open == 1 || user_open == 4) {
              var please = false;
              var my_info = res.data.result.my_info;
              var blessing_comment = res.data.result.blessing_comment;
            }
            that.setData({
              title: res.data.result.title,
              id: options.id,
              sign: (options.sign ? options.sign : ''),
              data: res.data.result,
              user_open: user_open,
              please: (please ? please : ''),
              my_info: (my_info ? my_info : ''),
              blessing_comment: (blessing_comment ? blessing_comment : ''),
              join_user_id: res.data.result.join_user_id,
              likegoods: res.data.result.goods_list,
              goods_nav_img: res.data.result.goods_nav_img,
            })
            if (res.data.result.my_info) {
              that.setData({
                sign: (res.data.result.my_info.sign == undefined ? '' : res.data.result.my_info.sign),
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
            })
          }
        }
      })

    }
  },
  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image= {
      width: viewWidth,
      height: viewHeight
    }
    console.log(image);
    this.setData({
      images: image
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    time_box = setInterval(function () {
      cal_tiem(that, that.data.times);
    }, 1000)
  },
// 下拉刷新
  onPullDownRefresh: function () {
    var options={};
    options.id = this.data.id;
    options.sign = (this.data.sign ? this.data.sign:'0');
    this.onLoad(options);
    wx.stopPullDownRefresh();
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var url = '/pages/blessing/index/index?id=' + that.data.id + '&sign=' + (that.data.sign == undefined ? '' : that.data.sign);
    console.log(url);
    return {
      title: that.data.title,
      path: url,
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (res) {
        console.log('转发失败');
      }
    }
  }
})

//时间倒计时
function cal_tiem(that, time) {
  if (time > 0) {
    var D, W, H, I, S;

    function fillZero(v) {
      if (v < 10) {
        v = '0' + v;
      }
      return v;
    }

    var d = new Date(parseInt(time) * 1000);
    D = fillZero(d.getDate());
    H = fillZero(d.getHours());
    I = fillZero(d.getMinutes());
    S = fillZero(d.getSeconds());
    var localTime = D + '天 ' + H + '时' + I + '分' + S;
    that.setData({
      times: --time,
      day: D,
      time: H,  //时间
      min: I,
    })


  } else {
    clearInterval(time_box);
    time_box = '';
  }
}